```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Java线程池的非核心线程生命周期</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 6px 15px rgba(0,0,0,0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 20px rgba(0,0,0,0.15);
        }
        .code-block {
            font-family: 'Courier New', monospace;
            background-color: #f8f9fa;
            border-radius: 6px;
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 6px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            opacity: 0.5;
            transition: all 0.3s ease;
        }
        .highlight:hover:after {
            height: 10px;
            opacity: 0.8;
        }
        .section-divider {
            border: none;
            height: 2px;
            background: linear-gradient(90deg, transparent, rgba(102, 126, 234, 0.5), transparent);
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 4.5em;
            line-height: 0.8;
            margin: 0.15em 0.15em 0.1em 0;
            color: #667eea;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <div class="hero text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 animate-fade-in-down">
                <i class="fas fa-cogs mr-4"></i>深入理解Java线程池的非核心线程生命周期
            </h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8 leading-relaxed">
                探索ThreadPoolExecutor如何智能管理非核心线程资源，优化系统性能
            </p>
            <div class="flex justify-center space-x-4">
                <div class="bg-white bg-opacity-20 px-4 py-2 rounded-full inline-flex items-center">
                    <i class="fas fa-clock mr-2"></i>
                    <span>keepAliveTime机制解析</span>
                </div>
                <div class="bg-white bg-opacity-20 px-4 py-2 rounded-full inline-flex items-center">
                    <i class="fas fa-microchip mr-2"></i>
                    <span>资源优化策略</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-6 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="drop-cap text-gray-700 text-lg leading-relaxed">
                在Java的线程池中，非核心线程的生命周期由线程池的配置参数<code class="code-block px-2 py-1 mx-1">keepAliveTime</code>控制。线程池通过定期检查非核心线程的活动状态来决定是否终止这些线程。在<code class="code-block px-2 py-1 mx-1">keepAliveTime</code>设定的时间内没有任务提交给线程池，非核心线程将被终止以释放资源。
            </div>
        </section>

        <hr class="section-divider my-12">

        <!-- Section 1 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 text-indigo-600 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-cog text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">ThreadPoolExecutor的工作机制</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white p-8 rounded-xl">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 text-indigo-600 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <span>1</span>
                        </div>
                        <h3 class="text-xl font-semibold">核心参数概述</h3>
                    </div>
                    <p class="text-gray-700 mb-4"><code class="code-block px-2 py-1 mx-1">ThreadPoolExecutor</code>是Java标准库中提供的一个强大线程池实现，它负责管理线程池中的核心线程和非核心线程。</p>
                    
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-circle text-xs text-indigo-500 mt-2 mr-2"></i>
                            <span><strong>核心线程数(corePoolSize)</strong>：线程池中保持的最小线程数量</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-circle text-xs text-indigo-500 mt-2 mr-2"></i>
                            <span><strong>最大线程数(maximumPoolSize)</strong>：线程池中允许的最大线程数量</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-circle text-xs text-indigo-500 mt-2 mr-2"></i>
                            <span><strong>线程空闲时间(keepAliveTime)</strong>：非核心线程在空闲时等待任务的最大时间</span>
                        </li>
                    </ul>
                </div>
                
                <div class="card bg-white p-8 rounded-xl">
                    <div class="flex items-center mb-4">
                        <div class="bg-indigo-100 text-indigo-600 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <span>2</span>
                        </div>
                        <h3 class="text-xl font-semibold">线程池工作原理可视化</h3>
                    </div>
                    <div class="mermaid">
                        graph TD
                            A[提交任务] --> B{核心线程<br>是否可用?}
                            B -->|是| C[使用核心线程执行]
                            B -->|否| D{工作队列<br>是否已满?}
                            D -->|否| E[任务入队列等待]
                            D -->|是| F{非核心线程<br>是否达到上限?}
                            F -->|否| G[创建非核心线程执行]
                            F -->|是| H[执行拒绝策略]
                    </div>
                </div>
            </div>
        </section>

        <hr class="section-divider my-12">

        <!-- Section 2 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 text-indigo-600 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-plus-circle text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">非核心线程的创建与管理</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="card bg-white p-6 rounded-lg">
                    <div class="text-indigo-600 mb-3">
                        <i class="fas fa-birthday-cake text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">线程创建</h3>
                    <p class="text-gray-700">当核心线程忙碌且线程总数未达上限时，创建新的非核心线程处理任务。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="text-indigo-600 mb-3">
                        <i class="fas fa-tasks text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">任务提交</h3>
                    <p class="text-gray-700">优先使用核心线程，核心线程忙碌则使用队列或创建非核心线程。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="text-indigo-600 mb-3">
                        <i class="fas fa-recycle text-2xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">线程维护</h3>
                    <p class="text-gray-700">空闲非核心线程在keepAliveTime后终止，有任务时优先重用而非创建新线程。</p>
                </div>
            </div>
            
            <div class="bg-indigo-50 p-6 rounded-xl border-l-4 border-indigo-500">
                <div class="flex">
                    <div class="mr-4 text-indigo-600">
                        <i class="fas fa-info-circle text-2xl"></i>
                    </div>
                    <div>
                        <h4 class="font-bold text-lg mb-2 text-indigo-700">关键点</h4>
                        <p class="text-gray-700">非核心线程的动态创建和销毁机制使得线程池能够根据负载情况自动调整线程数量，既保证了任务处理的及时性，又避免了资源浪费。</p>
                    </div>
                </div>
            </div>
        </section>

        <hr class="section-divider my-12">

        <!-- Section 3 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 text-indigo-600 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-code text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">ThreadPoolExecutor的生命周期方法</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">beforeExecute & afterExecute</h3>
                    <p class="text-gray-700 mb-6">ThreadPoolExecutor提供了这些钩子方法用于任务执行前后的操作，虽然它们不直接影响非核心线程生命周期，但在监控和扩展功能时非常有用。</p>
                    
                    <div class="bg-gray-800 text-gray-100 p-4 rounded-lg overflow-x-auto">
                        <pre><code class="language-java">
protected void beforeExecute(Thread t, Runnable r) {
    // 任务执行前的操作，如记录日志、初始化资源等
    System.out.println("准备执行任务: " + r);
}

protected void afterExecute(Runnable r, Throwable t) {
    // 任务执行后的操作，如清理资源、记录结果等
    super.afterExecute(r, t);
    if (t != null) {
        System.err.println("任务执行异常: " + t);
    }
}
                        </code></pre>
                    </div>
                </div>
                
                <div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-800">生命周期关系图</h3>
                    <div class="mermaid">
                        sequenceDiagram
                            participant User
                            participant ThreadPool
                            participant CoreThread
                            participant NonCoreThread
                            
                            User->>ThreadPool: 提交任务
                            ThreadPool->>CoreThread: 分配任务(优先)
                            CoreThread-->>ThreadPool: 任务完成
                            
                            alt 核心线程忙碌
                                ThreadPool->>NonCoreThread: 创建并分配任务
                                NonCoreThread-->>ThreadPool: 任务完成
                                loop 定期检查
                                    ThreadPool->>NonCoreThread: 检查空闲时间
                                    alt 空闲超过keepAliveTime
                                        ThreadPool->>NonCoreThread: 终止线程
                                    end
                                end
                            end
                    </div>
                </div>
            </div>
        </section>

        <hr class="section-divider my-12">

        <!-- Section 4 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 text-indigo-600 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-hourglass-end text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">非核心线程的终止过程</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="card bg-white p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-search text-indigo-500 mr-3"></i>
                        <span>线程空闲检测</span>
                    </h3>
                    <p class="text-gray-700">线程池使用<code class="code-block px-2 py-1 mx-1">ScheduledThreadPoolExecutor</code>的定时任务来定期检查线程池中非核心线程的空闲状态。如果线程空闲时间超过<code class="code-block px-2 py-1 mx-1">keepAliveTime</code>，且线程数超过核心线程数，则非核心线程将被终止。</p>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-power-off text-indigo-500 mr-3"></i>
                        <span>线程终止</span>
                    </h3>
                    <p class="text-gray-700">当非核心线程的空闲时间超过<code class="code-block px-2 py-1 mx-1">keepAliveTime</code>时，线程池将调用<code class="code-block px-2 py-1 mx-1">Thread.interrupt()</code>方法中断该线程。被中断的线程会进行正常的清理工作，然后终止自身。</p>
                </div>
            </div>
            
            <div class="bg-gradient-to-r from-indigo-50 to-purple-50 p-8 rounded-xl">
                <h3 class="text-2xl font-semibold mb-6 text-center text-gray-800">非核心线程生命周期流程</h3>
                <div class="mermaid">
                    flowchart LR
                        A[线程创建] --> B[执行任务]
                        B --> C{是否空闲?}
                        C -->|是| D[开始计时]
                        C -->|否| B
                        D --> E{空闲时间 > keepAliveTime?}
                        E -->|是| F[线程终止]
                        E -->|否| C
                </div>
            </div>
        </section>

        <hr class="section-divider my-12">

        <!-- Section 5 -->
        <section>
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 text-indigo-600 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-laptop-code text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">线程池的配置示例</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <div class="card bg-white p-6 rounded-lg mb-6">
                        <h3 class="text-xl font-semibold mb-4 flex items-center">
                            <i class="fas fa-cogs text-indigo-500 mr-3"></i>
                            <span>参数配置解析</span>
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <span class="bg-indigo-100 text-indigo-600 text-sm px-2 py-1 rounded mr-3">corePoolSize</span>
                                <span>2个核心线程常驻内存</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-indigo-100 text-indigo-600 text-sm px-2 py-1 rounded mr-3">maximumPoolSize</span>
                                <span>最多允许4个线程同时运行</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-indigo-100 text-indigo-600 text-sm px-2 py-1 rounded mr-3">keepAliveTime</span>
                                <span>非核心线程空闲60秒后终止</span>
                            </li>
                            <li class="flex items-start">
                                <span class="bg-indigo-100 text-indigo-600 text-sm px-2 py-1 rounded mr-3">工作队列</span>
                                <span>使用无界LinkedBlockingQueue</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 rounded-r">
                        <div class="flex">
                            <div class="flex-shrink-0 text-yellow-500 mr-3">
                                <i class="fas fa-exclamation-triangle"></i>
                            </div>
                            <div>
                                <h4 class="font-bold text-yellow-800">注意事项</h4>
                                <p class="text-yellow-700 mt-1">无界队列可能导致内存溢出，生产环境应使用有界队列并配置合理的拒绝策略。</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="bg-gray-800 text-gray-100 p-6 rounded-lg overflow-x-auto">
                    <pre><code class="language-java">
import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        // 创建一个线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
            2,                   // corePoolSize: 核心线程数
            4,                   // maximumPoolSize: 最大线程数
            60,                  // keepAliveTime: 非核心线程空闲时间
            TimeUnit.SECONDS,    // keepAliveTime 的时间单位
            new LinkedBlockingQueue<>() // 工作队列
        );

        // 提交任务到线程池
        executor.submit(() -> {
            System.out.println("Task executed by: " + Thread.currentThread().getName());
        });

        // 关闭线程池
        executor.shutdown();
    }
}
                    </code></pre>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            sequenceDiagram: {
                useMaxWidth: true,
                height: 300
            }
        });
        
        // 添加微交互效果
        document.querySelectorAll('a, button').forEach(el => {
            el.addEventListener('mouseenter', () => {
                el.classList.add('transition', 'duration-150');
            });
        });
    </script>
</body>
</html>
```